﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor
{
    public class RecentDirectoryListBoxItem : ListBoxItem
    {
        public RecentDirectoryListBoxItem(string directoryPath)
        {
            this.DirectoryPath = directoryPath;

            this.image.Source = new BitmapImage(new Uri("pack://application:,,,/LunarMarkdownEditor;component/Images/FolderHS.png"));
            this.image.Margin = new System.Windows.Thickness(0, 0, 4, 0);

            this.basePanel.Children.Add(image);
            DockPanel.SetDock(this.image, Dock.Left);
            this.basePanel.Children.Add(indexHeader);
            DockPanel.SetDock(this.indexHeader, Dock.Left);

            var stackPanel = new StackPanel() { Orientation = Orientation.Vertical, };
            stackPanel.Children.Add(this.header);
            stackPanel.Children.Add(this.comment);

            this.basePanel.Children.Add(stackPanel);

            this.Content = this.basePanel;
            this.ToolTip = "双击设置为当前工作区目录";
        }

        public string DirectoryPath
        {
            get { return this.comment.Text; }
            set
            {
                if (Directory.Exists(value))
                {
                    DirectoryInfo di = new DirectoryInfo(value);

                    if (di.FullName == Globals.DefaultWorkspacePath)
                    {
                        this.header.Text = "<备用文件源工作区>";
                    }
                    else {
                        this.header.Text = di.Name;
                    }
                    this.comment.Text = di.FullName;
                }
                else
                {
                    this.header.Text = "<非法路径>";
                    this.comment.Text = "文件不存在...";
                }
            }
        }

        public string IndexText
        {
            set { this.indexHeader.Text = value; }
        }

        private TextBlock indexHeader = new TextBlock()
        {
            VerticalAlignment = System.Windows.VerticalAlignment.Stretch,
            FontWeight = FontWeights.Bold,
            FontFamily = new FontFamily("Microsoft YaHei"),
            FontSize = 24,
            Margin = new Thickness(4, 2, 4, 2),
            Padding = new Thickness(4, 0, 4, 0),
        };

        private DockPanel basePanel = new DockPanel() { LastChildFill = true, };

        private TextBlock header = new TextBlock()
        {
            HorizontalAlignment = System.Windows.HorizontalAlignment.Left,
            Foreground = Brushes.Black,
        };

        public TextBlock Header
        {
            get { return header; }
            set { header = value; }
        }

        private TextBlock comment = new TextBlock()
        {
            HorizontalAlignment = System.Windows.HorizontalAlignment.Left,
            Foreground = Brushes.Black,
            Padding = new Thickness(14, 0, 0, 0),
        };

        private Image image = new Image() { Width = 16, Height = 16, VerticalAlignment = System.Windows.VerticalAlignment.Center, };
    }
}
